package com.project.pet.service;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.pet.common.ResponseData;
import com.project.pet.dto.InfoDto;
import com.project.pet.mapper.InfoMapper;
import com.project.pet.pojo.Info;
import com.project.pet.utils.ResponseDataUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InfoService {
    @Autowired
    private InfoMapper mapper;

    public ResponseData<Object> insert(Info info){
        info.setCreateTime(DateUtil.now());
        int res = mapper.insertSelective(info);
        return res>0? ResponseDataUtil.buildSuccess("操作成功"): ResponseDataUtil.buildError("操作失败");
    }
    public ResponseData<Object> updateByPrimaryKey(Info info){
        info.setUpdateTime(DateUtil.now());
        int res = mapper.updateByPrimaryKey(info);
        return res>0? ResponseDataUtil.buildSuccess("操作成功"): ResponseDataUtil.buildError("操作失败");
    }
    public ResponseData<Object> pagingQuery(HashMap<String,Object> map){
        Integer pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        map.remove("pageIndex");
        map.remove("pageSize");
        Example example = new Example(Info.class);
        Example.Criteria criteria = example.createCriteria();
        for(Map.Entry<String, Object> entry : map.entrySet()){
            String key = entry.getKey();
            Object value = entry.getValue();
            if(!StringUtils.isEmpty(value))
                criteria.andLike(key,"%"+value+"%");
        }
        PageHelper.startPage(pageIndex,pageSize);
        List<Info> list = mapper.selectByExample(example);
        List<InfoDto> res = new ArrayList<>();
        for (Info info : list) {
            InfoDto infoDto = new InfoDto();
            BeanUtils.copyProperties(info,infoDto);
            String[] covers = info.getCover().split("@");
            infoDto.setCoverList(covers);
            res.add(infoDto);
        }
        PageInfo<InfoDto> pageInfo = new PageInfo<>(res);
        return ResponseDataUtil.buildSuccess(pageInfo);
    }
    @Transactional
    public ResponseData<Object> delete(List<Integer> ids){
        try{
            for (Integer id : ids) {
                mapper.deleteByPrimaryKey(id);
            }
            return ResponseDataUtil.buildSuccess("操作成功");
        }catch (Exception e){
            return ResponseDataUtil.buildError("操作失败");
        }
    }
    public ResponseData<Object> selectAll(){
        Example example = new Example(Info.class);
        example.orderBy("createTime").desc();
        List<Info> list = mapper.selectByExample(example);
        List<InfoDto> res = new ArrayList<>();
        for (Info info : list) {
            InfoDto infoDto = new InfoDto();
            BeanUtils.copyProperties(info,infoDto);
            String[] covers = info.getCover().split("@");
            infoDto.setCoverList(covers);
            res.add(infoDto);
        }
        return ResponseDataUtil.buildSuccess(res);
    }
    public ResponseData<Object> selectByMap(HashMap<String,Object> map){
        Example example = new Example(Info.class);
        Example.Criteria criteria = example.createCriteria();
        for(Map.Entry<String, Object> entry : map.entrySet()){
            String key = entry.getKey();
            Object value = entry.getValue();
            if(!StringUtils.isEmpty(value))
                criteria.andEqualTo(key,value);
        }
        return ResponseDataUtil.buildSuccess(mapper.selectByExample(example));
    }
}
